<template>
  <div class="login-container">
    <div class="login-form">
      <div class="login-header">
        <!-- <img src="@/assets/images/logo.png" alt="Logo" class="logo" v-if="true"> -->
        <h2 class="title">经颅电治疗系统</h2>
        <p class="subtitle">后台管理</p>
      </div>
      
      <el-form 
        ref="loginFormRef" 
        :model="loginForm" 
        :rules="loginRules" 
        class="form"
        @keyup.enter="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            size="large"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="请输入密码"
            type="password"
            prefix-icon="Lock"
            size="large"
            show-password
          />
        </el-form-item>
        
        <el-form-item v-if="showCaptcha" prop="captcha">
          <div class="captcha-container">
            <el-input
              v-model="loginForm.captcha"
              placeholder="请输入验证码"
              prefix-icon="Key"
              size="large"
              class="captcha-input"
            />
            <div class="captcha-image" @click="getCaptcha">
              <img :src="captchaImage" alt="点击获取验证码" v-if="captchaImage">
              <span v-else>加载中...</span>
            </div>
          </div>
        </el-form-item>
        
        <el-form-item>
          <el-checkbox v-model="rememberMe">记住我</el-checkbox>
        </el-form-item>
        
        <el-form-item>
          <el-button
            :loading="loading"
            type="primary"
            size="large"
            class="login-button"
            @click="handleLogin"
            block
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        <p>© 2025. All rights reserved.</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import type { FormInstance } from 'element-plus'

// 路由和用户store
const router = useRouter()
const userStore = useUserStore()

// 表单引用
const loginFormRef = ref<FormInstance>()

// 登录状态
const loading = ref(false)

// 表单数据
const loginForm = reactive({
  username: 'admin',
  password: '123456',
  captcha: '',
  captchaId: ''
})

// 记住我
const rememberMe = ref(true)

// 验证码相关
const showCaptcha = ref(false)
const captchaImage = ref('')

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度为3-20个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度为6-20个字符', trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ]
}

// 获取验证码
const getCaptcha = () => {
  // 这里应该调用获取验证码的API
  // 暂时使用占位符
  captchaImage.value = ''
  setTimeout(() => {
    // 模拟获取验证码
    captchaImage.value = ''
  }, 300)
}

// 处理登录
const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (!valid) return
    
    loading.value = true
    
    try {
      // 调用登录接口
      await userStore.login({
        username: loginForm.username,
        password: loginForm.password
      })
      
      // 登录成功提示
      ElMessage.success('登录成功')
      
      // 跳转到首页
      router.push('/')
    } catch (error: any) {
      console.error('登录失败:', error)
      ElMessage.error(error.message || '登录失败')
      
      // 如果需要验证码则显示
      if (error.code === 'NEED_CAPTCHA') {
        showCaptcha.value = true
        getCaptcha()
      }
    } finally {
      loading.value = false
    }
  })
}

// 初始化
onMounted(() => {
  // 如果有记住的用户名则填充
  const savedUsername = localStorage.getItem('savedUsername')
  if (savedUsername) {
    loginForm.username = savedUsername
  }
})
</script>

<style scoped lang="scss">
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  
  .login-form {
    width: 100%;
    max-width: 400px;
    padding: 40px 30px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 10px;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
    
    .login-header {
      text-align: center;
      margin-bottom: 30px;
      
      .logo {
        width: 60px;
        height: 60px;
        margin-bottom: 15px;
      }
      
      .title {
        font-size: 24px;
        font-weight: 600;
        color: #333;
        margin: 0 0 10px 0;
      }
      
      .subtitle {
        font-size: 14px;
        color: #666;
        margin: 0;
      }
    }
    
    .form {
      :deep(.el-form-item) {
        margin-bottom: 20px;
      }
      
      :deep(.el-input__wrapper) {
        background: #f8f9fa;
        box-shadow: 0 0 0 1px #e9ecef inset;
        
        &:hover {
          box-shadow: 0 0 0 1px #ced4da inset;
        }
        
        &.is-focus {
          box-shadow: 0 0 0 1px #667eea inset;
        }
      }
    }
    
    .captcha-container {
      display: flex;
      gap: 10px;
      
      .captcha-input {
        flex: 1;
      }
      
      .captcha-image {
        width: 120px;
        height: 40px;
        border: 1px solid #dcdfe6;
        border-radius: 4px;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #f8f9fa;
        font-size: 12px;
        color: #666;
        
        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }
    }
    
    .login-button {
      margin-top: 10px;
      width: 100%;
    }
    
    .login-footer {
      text-align: center;
      margin-top: 30px;
      
      p {
        font-size: 12px;
        color: #999;
        margin: 0;
      }
    }
  }
}

// 响应式设计
@media (max-width: 576px) {
  .login-container {
    padding: 10px;
    
    .login-form {
      padding: 30px 20px;
    }
  }
}
</style>